﻿package  birdry.utils	{
	
	//	import birdry.utils.MovieClipUtil;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.display.MovieClip;
	import flash.events.Event;
	
	
	import org.papervision3d.Papervision3D;	
	import org.papervision3d.view.Viewport3D;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.materials.*;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.objects.DisplayObject3D;
	
	
	import gs.TweenMax;
	import gs.easing.Linear;
	import birdry.manager.TweenManager;
	import birdry.utils.ArrayUtil;
	
	public class TransitionPv3dUtil	extends Sprite	{
		
		public static function rotationCell( originObj:DisplayObject, targetObj:DisplayObject, width:uint, height:uint, func:Function = null, time:Number = 0.5, delay:Number = 1): void	{
			var i:uint;
			var u:uint;
			var viewport:Viewport3D;
			var scene:Scene3D;
			var camera:Camera3D;
			var renderer:BasicRenderEngine;
			var objArr:Array = new Array();
			
			var container:Sprite = new Sprite();
			Object(originObj.parent).addChild( container );
			container.x = originObj.x;
			container.y = originObj.y;
			
			
			targetObj.width = originObj.width;
			targetObj.height = originObj.height;
			
			var originBitmapArr:Array = BitmapUtil.returnBitmapSplit(originObj, width, height);
			var targetBitmapArr:Array = BitmapUtil.returnBitmapSplit(targetObj, width, height);
			
			viewport = new Viewport3D(originObj.width, originObj.height, false, true);
			
			container.addChild(viewport);
			
			//Papervision3D.VERBOSE = false;
			//------------------------	 	하나의 장면을 나타내는 씬, 관점을 나타내는 카메라		---------------------//
			//------------------------	 	그리고 뷰포트와 이 둘을 이용해 그리는 렌더러		---------------------//
			scene = new Scene3D();
			camera = new Camera3D();
			renderer = new BasicRenderEngine();
			camera.z = -1000;
			camera.zoom = 1000;
			camera.focus = 1;
			
			var startX:Number = -originObj.width / 2;
			var startY:Number = -originObj.height / 2;
			
			for (i = 0; i < originBitmapArr.length; ++i)	{
				for (u = 0; u < originBitmapArr[i].length; ++u)	{
					var displayObject3D:DisplayObject3D = new DisplayObject3D();
					
					scene.addChild( displayObject3D );
					displayObject3D.x = i * width + startX + (width/2);
					displayObject3D.y = -u * height - startY - (height/2);
					
					var planeFront:Plane = new Plane(new BitmapMaterial( originBitmapArr[i][u] ), width, height, 3, 3);
					var planeBack:Plane = new Plane(new BitmapMaterial( targetBitmapArr[i][u] ), width, height, 3, 3);
					
					displayObject3D.addChild(planeFront);
					displayObject3D.addChild(planeBack);
					planeBack.rotationY = 180;
					objArr.push(displayObject3D);
					
					
					//TweenMax.to(displayObject3D, time, { rotationX:180, delay:Math.random() * delay, ease:TweenManager.Quad_easeInOut } );
				}
			}
			
			ArrayUtil.shuffle(objArr);
			for (i = 0; i < objArr.length; ++i)	{
				TweenMax.to(objArr[i], time, { rotationY:180, delay:i*delay, ease:TweenManager.Back_easeInOut} );
			}
			
			//originObj.visible = false;
			//targetObj.visible = false;
			
			var movieClip:MovieClip = new MovieClip();
			movieClip.addEventListener(Event.ENTER_FRAME, onEnter);
			
			function onEnter( e: Event ): void	{
				renderer.renderScene(scene, camera, viewport);
			}
			
			TweenMax.to(movieClip, originBitmapArr.length*delay+time, { onComplete:transitionComplete } );
			
			function transitionComplete(): void	{
				movieClip.removeEventListener(Event.ENTER_FRAME, onEnter);
				DisplayObjectUtil.removeDisplayObject(container);
				//targetObj.visible = true;
				if(func != null)	func();
			}
		}
	}
}

